package com.icss.service.bankmanage;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.icss.service.BaseService;

/**
 * 行名行号管理
 * @author shixiaolong
 * @dateJun 12, 2012
 */
public class BankManageService extends BaseService{
	
	public List<Map> getList(HttpServletRequest request){
		String page_num = req.getValue(request, "page_num");
		String bank_id = req.getValue(request, "bank_id");
		String bank_name = req.getValue(request, "bank_name");
		String state = req.getValue(request, "state");
		String sql="select bank_id,bank_name,decode(state,1,'有效',0,'冻结') state,city_name,jion_sname,jion_aname,address,portcode,phone,email," +
				"bank_cash,to_char(begin_date,'yyyy-mm-dd') begin_date,to_char(end_date,'yyyy-mm-dd') end_date " +
				"from tbl_bank a where 1 = 1 ";
		if(!"".equals(bank_id)){
			sql+=" and bank_id like  '%"+bank_id+"%'";
		}
		if(!"".equals(bank_name)){
			sql+=" and bank_name like '%"+bank_name+"%'";
		}
		if(!"".equals(state)){
			sql+=" and state = "+state;
		}
		return db.getForList(sql, Integer.parseInt(page_num), request);
	}
	
	/**
	 * @param request
	 * @return
	 */
	public int save(HttpServletRequest request){
		String bank_name = req.getValue(request, "bank_name");
		String state = req.getValue(request, "state");
		String city_name = req.getValue(request, "city_name");
		String jion_sname = req.getValue(request, "jion_sname");
		String jion_aname = req.getValue(request, "jion_aname");
		String address = req.getValue(request, "address");
		String portcode = req.getValue(request, "portcode");
		String phone = req.getValue(request, "phone");
		String email = req.getValue(request, "email");
		String begin_date = req.getValue(request, "begin_date");
		String end_date = req.getValue(request, "end_date");
		String bank_id = req.getValue(request, "bank_id");
		String method = req.getValue(request, "method");
		String bank_cash = req.getValue(request, "bank_cash");
		String sql="";
		if("create".equals(method)){
			String sequence_bank_id = db.queryForString("select bank_id_s_id.nextval from dual");
			if(sequence_bank_id.length()==1){
				bank_id = "19000"+sequence_bank_id;
			}else if(sequence_bank_id.length()==2){
				bank_id = "1900"+sequence_bank_id;
			}else if(sequence_bank_id.length()==3){
				bank_id = "190"+sequence_bank_id;
			}
			sql="insert into tbl_bank(bank_id,bank_name,state,city_name,jion_sname," +
					"jion_aname,address,portcode,phone,email,begin_date,end_date,bank_cash) " +
					"values(?,?,?,?,?,?,?,?,?,?,to_date(?,'yyyy-mm-dd'),to_date(?,'yyyy-mm-dd'),?)";
			log.debug(str.getSql(sql,new Object[]{bank_id,bank_name,state,city_name,jion_sname,
					jion_aname,address,portcode,phone,email,begin_date,end_date,bank_cash}));
			return db.update(sql,new Object[]{bank_id,bank_name,state,city_name,jion_sname,
					jion_aname,address,portcode,phone,email,begin_date,end_date,bank_cash});
		}else{
			sql="update tbl_bank set bank_name=?,state=?,city_name=?,jion_sname=?," +
					"jion_aname=?,address=?,portcode=?,phone=?,email=?,begin_date=to_date(?,'yyyy-mm-dd')," +
					"end_date=to_date(?,'yyyy-mm-dd'),bank_cash=? where bank_id=?";
			return db.update(sql,new Object[]{bank_name,state,city_name,jion_sname,jion_aname
					,address,portcode,phone,email,begin_date,end_date,bank_cash,bank_id});
		}
	}
	
	public Map getDetail(HttpServletRequest request){
		String bank_id = req.getValue(request, "bank_id");
		System.out.println(bank_id+"-----------------------");
		String sql="select a.*,to_char(begin_date,'yyyy-mm-dd') begin_date,to_char(end_date,'yyyy-mm-dd') end_date from tbl_bank a where bank_id = ?";
		return db.queryForMap(sql,new Object[]{bank_id});
		
	}
	
	public int delete(HttpServletRequest request){
		String bank_id = req.getValue(request, "bank_id");
		String sql="delete from tbl_bank where bank_id = ? ";
		return db.update(sql,new Object[]{bank_id});
	}
	
	/////////////////////////////////////////以下是支付节点
	public List<Map> getBankHeadList(HttpServletRequest request){
		String type = req.getValue(request, "type");
		String bh_name = req.getValue(request, "bh_name");
		String page_num = req.getValue(request, "page_num");
		String sql="select * from tbl_bank_head where 1=1 ";
		if(!"".equals(type)){
			sql+=" and type like '%"+type+"%'";
		}
		if(!"".equals(bh_name)){
			sql+=" and bh_name like '%"+bh_name+"%'";
		}
		return db.getForList(sql, Integer.parseInt(page_num), request);
	}
}
